package me.caofeng.service;

import me.caofeng.dao.AddressMapper;
import me.caofeng.enums.ResponseCode;
import me.caofeng.pojo.Address;
import me.caofeng.pojo.Response;
import me.caofeng.service.api.IAddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * Copyright © 2017曹峰. All rights reserved.
 *
 * @Prject: mall
 * @Package: me.caofeng.service
 * @Description:
 * @Author: 曹峰 blog.caofeng.me
 * @Date: 2017-06-27 17:08
 * @Version: V1.0
 */
@Service
@Transactional
public class AddressService implements IAddressService{

    @Autowired
    private AddressMapper addressMapper;

    @Override
    public Response<Address> addAddress(Address address) {
        int row = addressMapper.addAddress(address);
        if ( row != 1){
            return Response.response(ResponseCode.ERROR,"添加地址失败",null);
        }
        return Response.response(ResponseCode.SUCCESS,"添加地址成功",address);
    }

    @Override
    public Response deleteAddress(Integer id, String userId) {
       int row = addressMapper.deleteAddress(id,userId);

       if (row != 1){
           return Response.response(ResponseCode.ERROR,"删除地址失败",null);
       }

       return Response.response(ResponseCode.SUCCESS,"删除地址成功",null);
    }

    @Override
    public Response<Address> updateAddress(Integer id, String userId, Address address) {
        address.setUserId(userId);
        address.setId(id);
        int row = addressMapper.updateAddress(address);

        if ( row != 1){
            return Response.response(ResponseCode.ERROR,"修改地址信息失败",null);
        }

        return Response.response(ResponseCode.SUCCESS,"修改地址信息成功",address);
    }

    @Override
    public Response<Address> getAddress(Integer id, String userId) {
        Address address = addressMapper.getAddress(id,userId);
        if (null == address){
            return Response.response(ResponseCode.EMPTY,"获取地址成功",null)   ;
        }
        return Response.response(ResponseCode.SUCCESS,"获取地址信息成功",address);
    }

    @Override
    public Response<List<Address>> getAddresses(String userId) {
        List<Address> addresses = addressMapper.getAddresses(userId);
        if (null == addresses || addresses.isEmpty()){
            return Response.response(ResponseCode.EMPTY,"获取收货地址成功",null);
        }
        return Response.response(ResponseCode.SUCCESS,"获取收获地址成功",addresses);
    }


}
